/*
 * @Author: ckk
 * @Date: 2022-08-18 13:10:35
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-08-25 14:53:11
 * @Description: your project
 * @version: 1.0
 */
import axios from './request';
import yaxios from 'axios';
// const baseUrl = 'https://zw.czbanbantong.com';
/**
 * @description: 获取openid
 * @param {*} params
 * @Author: ckk
 * @Date: 2022-08-18 13:24:22
 * @return {*}
 */
export async function getOpneId (params) {
  const { data } = await axios.get(
    `/sso/auth/wechat/code/openid/${params.code}`
  );
  return data || {};
}
/**
 * @description: 公众号认证
 * @param {*} params
 * @Author: ckk
 * @Date: 2022-08-18 14:13:48
 * @return {*}
 */

export async function useAttestation (params) {
  const Qs = require('qs'),
  Base64 = require('js-base64').Base64,
  str = 'wechart-official-account:f8d498180ba701b71ac2f421fc4c2142';
  try {
    const data = await yaxios({
        method: 'POST',
        url: '/sso/oauth/service/token?grant_type=weixin_official_student_parent',
        headers: {
          Authorization: 'Basic ' + Base64.encode(str),
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: Qs.stringify(params)
      });
      console.log(data.dat.data);
    return data.data || {};
  } catch (error) {
    console.log(error.response);
    console.log(error.response.data, 'data');
    return error.response.data;
  }
}
// 重新认证
// eslint-disable-next-line camelcase
export async function accessReauth ({ access_token }) {
  const { data } = await axios.post(
    '/sso/oauth/service/token?grant_type=access_token_reauth',
    { access_token },
    {
      headers: {
        Authorization: 'Basic cG9ydGFsOmY2NmI3ZDc4OGM4NDRlODRkYjJkNmY4ZjQ4MjhhZDA2'
      },
      contentType: 'application/x-www-form-urlencoded',
      transformRequest: [
        function (data) {
          let ret = '';
          for (const it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
          }
          return ret;
        }
      ]
    }
  );
  return data;
}
